clear;
clc

Dataname = '3sources3vbigRnSp';
Datafold = [Dataname,'_percentDel_0.1.mat'];

para_r = 5;
para_k = 9;
lambda1 = 0.00001;
lambda2 = 0.1;
lambda3 = 0.1;

for f = 1:5
    load(Dataname);
    load(Datafold);
    ind_folds = folds{f};
    truthF = truth;
    numClust = length(unique(truthF));
    num_view = length(X);
    NumSamp = length(truthF);
    if size(X{1},2)~=NumSamp
        for iv = 1:num_view
            X{iv} = X{iv}';
        end
    end
    clear Y
    linshi_WW = 0;
    for iv = 1:num_view
        X1 = X{iv};
        X1 = NormalizeFea(X1,0);
        ind_0 = find(ind_folds(:,iv) == 0);
        X1(:,ind_0) = [];
        Y{iv} = X1;
        W1 = eye(size(ind_folds,1));
        W1(ind_0,:) = [];
        G{iv} = W1;
        linshi_W = ones(NumSamp,NumSamp);
        linshi_W(ind_0,:) = 0;
        linshi_W(:,ind_0) = 0;
        Wiv{iv} = linshi_W;
        clear ind_0
        linshi_WW = linshi_WW+Wiv{iv};
    end
    clear X X1 W1
    X = Y;
    clear Y
    for iv = 1:num_view
        options = [];
        options.NeighborMode = 'KNN';
        options.k = para_k;
        options.WeightMode = 'HeatKernel';  % HeatKernel  Binary
        Z1 = full(constructW(X{iv}',options));
        S_ini{iv} = G{iv}'*Z1*G{iv};
        clear Z1
    end

    F_ini = solveF(S_ini,numClust);
    max_iter = 100;

    [F,Z,obj] = WCGL_IMVC(S_ini,Wiv,F_ini,para_r,numClust,lambda1,lambda2,lambda3,max_iter);
    new_F = F;
    norm_mat = repmat(sqrt(sum(new_F.*new_F,2)),1,size(new_F,2));
    for i = 1:size(norm_mat,1)
        if (norm_mat(i,1)==0)
            norm_mat(i,:) = 1;
        end
    end
    new_F = new_F./norm_mat;

    pre_labels    = kmeans(real(new_F),numClust,'emptyaction','singleton','replicates',20,'display','off');
    result_cluster = ClusteringMeasure(truthF, pre_labels)*100;
    acc(f) = result_cluster(1);
end

mean_acc = mean(acc)
